package spass.renderer;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

public class Color {

    public static final Color RED = new Color(1, 0, 0);
    public static final Color BLUE = new Color(0, 1, 0);
    public static final Color GREEN = new Color(0, 0, 1);
    public static final Color BLACK = new Color(0, 0, 0);
    public static final Color WHITE = new Color(1, 1, 1);

    static final String VALIDATION_ERROR_MSG = "color parts must lie between 0.0 and 1.0";

    private final double r;
    private final double b;
    private final double g;

    public Color(double r, double b, double g) {
        validateRange(r);
        validateRange(b);
        validateRange(g);

        this.r = r;
        this.b = b;
        this.g = g;
    }

    private void validateRange(double colorPart) {
        if (colorPart < 0.0 || colorPart > 1.0) {
            throw new IllegalArgumentException(VALIDATION_ERROR_MSG);
        }
    }

    public Color scale(double factor) {
        return new Color(r * factor, b * factor, g * factor);
    }

    public double getR() {
        return r;
    }

    public double getB() {
        return b;
    }

    public double getG() {
        return g;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Color) {
            Color that = (Color) obj;

            return new EqualsBuilder().append(this.r, that.r).append(this.g, that.g).append(this.b, that.b).isEquals();
        }

        return false;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(r).append(b).append(g).toHashCode();
    }

}
